#!/usr/local/bin/node
var
    sys = require('sys'),
    net = require('net'),
    amqp = require('./index');

var
    channel = amqp.channel({ host: '192.168.10.147' }),
    exchange = amqp.exchange(channel),
    queue = amqp.queue(channel),
    basic = amqp.basic(channel);

var queueName = 'queue1';
var exchangeName = 'p2p';

channel.addListener('open', function(){
    sys.debug('channel opened');
    queue.declare(0, queueName, false, true, false, false, false, {});
});

queue.addListener('declare', function(){
    sys.debug('queue declared');
    queue.bind({
        ticket: channel.accessTicket,
        queue: queue.name,
        exchange: exchange.name,
        routingKey: '*',
        nowait: false,
        arguments: {}
    });
});

queue.addListener('bind', function(){
    sys.debug('queue binded');
    basic.consume({
        ticket: channel.accessTicket,
        queue: queue.name,
        consumerTag: 'tag-1',
        noLocal: false,
        noAck: true,
        exclusive: false,
        nowait: false
    });
});

basic.addListener('consume', function(){
    sys.debug('content consumed');
    basic.publish({
        method: {
            arguments: {
                ticket: channel.accessTicket,
                exchange: exchange.name,
                routingKey: 'engine',
                mandatory: false,
                immediate: false
            }
        },
        header: {
            fields: {
                contentType: 'application/xml'
            }
        },
        body: '<event>test</event>'
    });
});

basic.addListener('deliver', function(frame){
    if ('application/xml' == frame.header.fields.contentType) {
        sys.debug('content delivered');
        sys.debug(sys.inspect(frame));
    }
});

channel.addListener('close', function(frame){
    sys.debug('channel closed');
    var Amqp = require('./api/amqp');
    if ('classId' in frame.method.arguments && 'methodId' in frame.method.arguments) {
        sys.puts('CHANNEL ERROR [' + Amqp.Class[frame.method.arguments.classId] + '.' +
                Amqp.Class[frame.method.arguments.classId][frame.method.arguments.methodId] + ']\n' +
                frame.method.arguments.replyCode + ' ' + frame.method.arguments.replyText);
    }
});

channel._handler.addListener('close', function(frame){
    sys.debug('connection closed');
    var Amqp = require('./api/amqp');
    if ('classId' in frame.method.arguments && 'methodId' in frame.method.arguments &&
            frame.method.arguments.classId in Amqp.Class && frame.method.arguments.methodId in Amqp.Class[classId]) {
        sys.puts('CONNECTION ERROR [' + Amqp.Class[frame.method.arguments.classId] + '.' +
                Amqp.Class[frame.method.arguments.classId][frame.method.arguments.methodId] + ']\n' +
                frame.method.arguments.replyCode + ' ' + frame.method.arguments.replyText);
    } else {
        sys.puts('UNKNOWN ERROR\n' + sys.inspect(frame, false, null));
    }
});

//var frame = {
//    channel: channelId,
//    method: {
//        classId: Amqp.Class.Connection,
//        methodId: Amqp.Method.Connection.Open,
//        arguments: {
//            routingKey: '11.12.13',
//            arg2: value2
//        }
//    },
//    header: {
//        field1: value1,
//        field2: value2
//    },
//    body: frameBody,
//    children: [{
//        header: {
//            field1: value1,
//            field2: value2
//        },
//        body: frameBody,
//        children: [{
//            header: {
//                field1: value1,
//                field2: value2
//            },
//            body: frameBody
//        }, {
//            header: {
//                field1: value1,
//                field2: value2
//            },
//            body: frameBody
//        }]
//    }, {
//        header: {
//            field1: value1,
//            field2: value2
//        },
//        body: frameBody
//    }]
//};
